class Solution {
public:
    vector<int> topKFrequent(vector<int>& nums, int k)
    {
        int n = nums.size();
        vector<int> ret;
        unordered_map<int, int> hash;
        for (auto& x : nums)
        {
            hash[x]++;
        }

        priority_queue<pair<int, int>> q;
        for (auto& i : hash)
        {
            q.push({ i.second,i.first });
        }
        for (int i = 0; i < k; i++)
        {
            ret.push_back(q.top().second);
            q.pop();
        }
        return ret;

    }
};